package com.study.demo.widget;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.study.demo.R;

/**
 * Created by sll on 2016/4/7.
 */
public class LoadMoreRecyclerView extends RecyclerView {
  /**
   * item 类型
   */
  public final static int TYPE_NORMAL = 0;
  public final static int TYPE_FOOTER = 1;
  public final static int TYPE_LIST = 2;
  public final static int TYPE_STAGGER = 3;

  private boolean mIsFooterEnable = false;//是否允许加载更多

  private AutoLoadAdapter mAutoLoadAdapter;

  private boolean mIsLoadingMore;
  private int mLoadMorePosition;
  private LoadMoreListener mListener;

  public LoadMoreRecyclerView(Context context) {
    this(context, null);
  }

  public LoadMoreRecyclerView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public LoadMoreRecyclerView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
  }

  private void init() {
    addOnScrollListener(new OnScrollListener() {

      @Override
      public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
      }

      @Override
      public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        if (null != mListener && mIsFooterEnable && !mIsLoadingMore && dy > 0) {
          int lastVisiblePosition = getLastVisiblePosition();
          if (lastVisiblePosition + 1 == mAutoLoadAdapter.getItemCount()) {
            setLoadingMore(true);
            mLoadMorePosition = lastVisiblePosition;
            mListener.onLoadMore();
          }
        }
      }
    });
  }

  /**
   * 设置加载更多的监听
   */
  public void setLoadMoreListener(LoadMoreListener listener) {
    mListener = listener;
  }

  /**
   * 设置正在加载更多
   */
  public void setLoadingMore(boolean loadingMore) {
    this.mIsLoadingMore = loadingMore;
  }

  /**
   * 加载更多监听
   */
  public interface LoadMoreListener {
    /**
     * 加载更多
     */
    void onLoadMore();
  }

  /**
   *
   */
  public class AutoLoadAdapter extends Adapter<ViewHolder> {

    /**
     * 数据adapter
     */
    private Adapter mInternalAdapter;

    public AutoLoadAdapter(Adapter adapter) {
      mInternalAdapter = adapter;
    }

    @Override
    public int getItemViewType(int position) {
      int footerPosition = getItemCount() - 1;

      if (footerPosition == position && mIsFooterEnable) {
        return TYPE_FOOTER;
      }
      if (getLayoutManager() instanceof LinearLayoutManager) {
        return TYPE_LIST;
      } else if (getLayoutManager() instanceof StaggeredGridLayoutManager) {
        return TYPE_STAGGER;
      } else {
        return TYPE_NORMAL;
      }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      if (viewType == TYPE_FOOTER) {
        return new FooterViewHolder(LayoutInflater.from(parent.getContext())
            .inflate(R.layout.layout_foot_loading, parent, false));
      } else { // type normal
        return mInternalAdapter.onCreateViewHolder(parent, viewType);
      }
    }

    public class FooterViewHolder extends ViewHolder {

      public FooterViewHolder(View itemView) {
        super(itemView);
      }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
      int type = getItemViewType(position);
      if (type != TYPE_FOOTER) {
        mInternalAdapter.onBindViewHolder(holder, position);
      }
    }

    @Override
    public int getItemCount() {
      int count = mInternalAdapter.getItemCount();
      if (mIsFooterEnable) count++;

      return count;
    }
  }

  @Override
  public void setAdapter(Adapter adapter) {
    if (adapter != null) {
      mAutoLoadAdapter = new AutoLoadAdapter(adapter);
    }
    super.swapAdapter(mAutoLoadAdapter, true);
  }

  /**
   * 获取最后一条展示的位置
   */
  private int getLastVisiblePosition() {
    int position;
    if (getLayoutManager() instanceof LinearLayoutManager) {
      position = ((LinearLayoutManager) getLayoutManager()).findLastVisibleItemPosition();
    } else if (getLayoutManager() instanceof GridLayoutManager) {
      position = ((GridLayoutManager) getLayoutManager()).findLastVisibleItemPosition();
    } else if (getLayoutManager() instanceof StaggeredGridLayoutManager) {
      StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) getLayoutManager();
      int[] lastPositions =
          layoutManager.findLastVisibleItemPositions(new int[layoutManager.getSpanCount()]);
      position = getMaxPosition(lastPositions);
    } else {
      position = getLayoutManager().getItemCount() - 1;
    }
    return position;
  }

  /**
   * 获得最大的位置
   */
  private int getMaxPosition(int[] positions) {
    int size = positions.length;
    int maxPosition = Integer.MIN_VALUE;
    for (int i = 0; i < size; i++) {
      maxPosition = Math.max(maxPosition, positions[i]);
    }
    return maxPosition;
  }

  /**
   * 设置是否支持自动加载更多
   */
  public void setLoadMoreEnable(boolean autoLoadMore) {
    mIsFooterEnable = autoLoadMore;
  }

  public void onLoadCompleted(boolean hasMore) {
    setLoadMoreEnable(hasMore);
    if (mIsLoadingMore) {
      getAdapter().notifyItemRemoved(mLoadMorePosition);
    }
    mIsLoadingMore = false;
  }
}
